<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Summary extends Model
{
    protected $table = 'summary';

    protected $fillable = [
    	'vehicle_number',
    	'driver_name',
    	'tare',
        'first_weight',
        'empty_weight_time',
        'weight_error_ratio',
    	'empty_type',
    	'net_weight',
    	'purchase_weight',
    	'discharge_weight',
    	'is_void',
    	'empty_weight',
    	'weight_error',
    	'pick_weight',
    	'is_success',
    	'is_return',
    	'is_go',
    	'exception',
        'first_readic_datetime',
        'is_finished'
    ];

    protected $appends = [
    	'is_void_desc',
        'empty_type_desc',
        'relative_weight',
        'is_return_desc',
        'is_go_desc',
        'is_success_desc',
        'is_finished_desc'
    ];

    const IS_SUCCESS_YES = 'Y';

    const IS_SUCCESS_NO = 'N';

    public static $IS_SUCCESS = [
        self::IS_SUCCESS_YES => '出厂过磅成功',
        self::IS_SUCCESS_NO => '出厂过磅失败'
    ];

    const IS_FINISHED_YES = 'Y';

    const IS_FINISHED_NO = 'N';

    public static $IS_FINISHED = [
        self::IS_FINISHED_YES => '是',
        self::IS_FINISHED_NO => '否'
    ];

    public function getIsFinishedDescAttribute(){
        if(array_key_exists('is_finished', $this->attributes) && $this->attributes['is_finished'] != null){
            return $this->attributes['is_finished_desc'] = self::$IS_FINISHED[$this->attributes['is_finished']];
        }
    }

    public function getIsSuccessDescAttribute(){
        if(array_key_exists('is_success', $this->attributes) && $this->attributes['is_success'] != null){
            return $this->attributes['is_success_desc'] = self::$IS_SUCCESS[$this->attributes['is_success']];
        }
    }

    public function getRelativeWeightAttribute(){
        if(array_key_exists('purchase_weight', $this->attributes)){
            if(array_key_exists('discharge_weight', $this->attributes)){
                return $this->attributes['relative_weight'] = $this->attributes['purchase_weight']-$this->attributes['discharge_weight'];
            }
        }
    }

    const IS_GO_ZERO = '0';

    const IS_GO_ONE = '1';

    public static $IS_GO = [
        self::IS_GO_ZERO => '不允许出门',
        self::IS_GO_ONE => '允许出门'
    ];

    public function getIsGoDescAttribute(){
        if(array_key_exists('is_go', $this->attributes) && isset($this->attributes['is_go'])){
            return $this->attributes['is_go_desc'] = self::$IS_GO[$this->attributes['is_go']];
        }
    }

    const IS_RETURN_ZERO = '0';

    const IS_RETURN_ONE = '1';

    public static $IS_RETURN = [
        self::IS_RETURN_ZERO => '否',
        self::IS_RETURN_ONE => '是'
    ];

    public function getIsReturnDescAttribute(){
        if(array_key_exists('is_return', $this->attributes) && isset($this->attributes['is_return'])){
            return $this->attributes['is_return_desc'] = self::$IS_RETURN[$this->attributes['is_return']];
        }
    }

    const IS_VOID_ZERO = '0';

    const IS_VOID_ONE = '1';

    public static $IS_VOID = [
        self::IS_VOID_ZERO => '否',
        self::IS_VOID_ONE => '是'
    ];

    public function getIsVoidDescAttribute(){
        if(array_key_exists('is_void', $this->attributes) && isset($this->attributes['is_void'])){
            return $this->attributes['is_void_desc'] = self::$IS_VOID[$this->attributes['is_void']];
        }
    }

    const EMPTY_TYPE_ZERO = '0';

    const EMPTY_TYPE_ONE = '1';

    const EMPTY_TYPE_TWO = '2';

    public static $EMPTY_TYPE = [
        self::EMPTY_TYPE_ZERO => '装货',
        self::EMPTY_TYPE_ONE => '卸货',
        self::EMPTY_TYPE_TWO => '无操作数据'
    ];

    public function getEmptyTypeDescAttribute(){
        if(array_key_exists('empty_type', $this->attributes) && $this->attributes['empty_type'] != null){
            return $this->attributes['empty_type_desc'] = self::$EMPTY_TYPE[$this->attributes['empty_type']];
        }
    }
}
